package com.winchain.provider;

import com.shell.bee.base.utils.StringUtil;
import com.shell.framework.utils.MessageSender;
import com.shell.governor.modules.message.keyword.TbExpKeyWord;
import com.winchain.chainbill.basic.cus.po.CusRec;
import com.winchain.chainbill.basic.cus.service.CusRecService;
import com.winchain.chainbill.tbill.base.po.TbRec;
import com.winchain.chainbill.tbill.base.po.TbSnd;
import com.winchain.chainbill.tbill.base.service.TbRecService;
import com.winchain.chainbill.tbill.base.service.TbSndService;
import java.math.BigDecimal;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class BillDctExpProvider {
	private Log logger = LogFactory.getLog(getClass());
	@Autowired
	private TbRecService tbRecService;
	@Autowired
	private CusRecService cusRecService;
	@Autowired
	private TbSndService tbSndService;

	@RequestMapping({ "/billDctExp/billTbDctExpTip" })
	public void billTbDctExpTip(@RequestParam("wordDate") String wordDate) {
		this.logger.info("====scheduler start wordDate====" + wordDate);
		TbRec tbRec = new TbRec();
		tbRec.setExpDate(wordDate);
		List<TbRec> tbRecs = this.tbRecService.selectByEntity(tbRec);
		for (TbRec rec : tbRecs) {
			TbSnd tbSnd = this.tbSndService.selectByBillId(rec.getBillId());
			if (tbSnd != null) {
				CusRec cusRec = (CusRec) this.cusRecService.selectByKey(tbSnd.getBuyerId());
				sendSmsMessage(rec.getBillCode(), rec.getBillAmt().toString(), cusRec.getCusName(), "MT09015",
						rec.getFinId());
				sendSmsMessage(rec.getBillCode(), rec.getBillAmt().toString(), null, "MT09016", tbSnd.getBuyerId());
			}
		}
	}

	private void sendSmsMessage(String billCode, String billAmt, String buyer, String messageTplId, String entId) {
		try {
			TbExpKeyWord keyword = new TbExpKeyWord();
			keyword.setBillCode(billCode);
			keyword.setBillAmt(billAmt);
			if (!StringUtil.emptyAndNull(buyer)) {
				keyword.setBuyer(buyer);
			}
			MessageSender.sendMessageByTmpl("", messageTplId, keyword, entId);
		} catch (Exception e) {
			this.logger.info("===============================" + billCode);
			this.logger.error("BillDctExpProvider ������������!", e);
		}
	}
}
